package com.ice.work.moment.socket.service.impl;

import com.honeybee.cloud.framework.base.exception.business.BaseBizException;
import com.honeybee.cloud.framework.common.constant.CommonMessageConstant;
import com.ice.work.moment.facade.socket.dto.PushUserDTO;
import com.ice.work.moment.facade.socket.mapper.PushUserConvertMapper;
import com.ice.work.moment.facade.socket.model.PushUser;
import com.ice.work.moment.socket.dao.user.PushUserRepository;
import com.ice.work.moment.socket.service.IPushUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert;

import java.util.Date;

/**
 * @Description 在线用户
 * @Author DELL
 * @Create 2019-04-17
 * @Since 1.0.0
 */
@Service
@SuppressWarnings("all")
public class PushUserServiceImpl implements IPushUserService {

    @Autowired
    private PushUserRepository pushUserRepository;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean add(PushUser pushUser) throws BaseBizException {
        Assert.notNull(pushUser, "pushUser:" + CommonMessageConstant.PARAM_IS_NULL);
        Assert.notNull(pushUser.getId(), "pushUser-->id:" + CommonMessageConstant.PARAM_IS_NULL);
        Assert.notNull(pushUser.getPushAuthorizationId(), "pushUser-->pushAuthorizationId:" + CommonMessageConstant.PARAM_IS_NULL);
        pushUser.setOnline(true);
        pushUser.setUpAt(new Date());
        Boolean isSuccess = pushUserRepository.save(pushUser);
        if (!isSuccess) {
            return isSuccess;
        }
        return isSuccess;
    }



    @Override
    public void logout(Long pushId) throws BaseBizException {
        Assert.notNull(pushId, "pushId:" + CommonMessageConstant.PARAM_IS_NULL);
        pushUserRepository.removeById(pushId);
    }

    @Override
    public PushUserDTO findByPushId(Long pushId) throws BaseBizException {
        Assert.notNull(pushId, "pushId:" + CommonMessageConstant.PARAM_IS_NULL);
        PushUser pushUser = pushUserRepository.getById(pushId);
        return PushUserConvertMapper.INSTANCE.toDTO(pushUser);
    }
}
